import urls from '/util/urls' 
const app = getApp()

export default {
  get(url, params, ok, fail) {
    if (typeof params === "function") {
      fail = ok
      ok = params
    }
    dd.httpRequest({
      url: url,
      method: 'GET',
      data: params,
      dataType: 'json',
      headers: {
        Authentication: app.globalData.token
      },
      success: (res) => {
          if(res.data.status === 'failure') {
            fail(res.data.data)
          } else {
            ok(res.data.data)
          }
      },
      fail: (res) => {
          // dd.alert({content: JSON.stringify(res)});
      }
    })
  },
  post(url, data, ok, fail) {
    dd.httpRequest({
      url: url,
      method: 'POST',
      data: JSON.stringify(data),
      dataType: 'json',
      headers: {
        Authentication: app.globalData.token,
        'Content-Type': 'application/json;charset=UTF-8'
      },
      success: (res) => {
          if(res.data.status === 'failure') {
            fail(res.data.data)
          } else {
            ok(res.data.data)
          }
      },
      fail: (res) => {
          fail()
      }
    })
  },
  put(url, data, ok, fail) {
    dd.httpRequest({
      url: url,
      method: 'POST',
      data: JSON.stringify(data),
      dataType: 'json',
      headers: {
        Authentication: app.globalData.token,
        'Content-Type': 'application/json;charset=UTF-8'
      },
      success: (res) => {
          if(res.data.status === 'failure') {
            fail(res.data.data)
          } else {
            ok(res.data.data)
          }
      },
      fail: (res) => {
          fail()
      }
    })
  },
  delete(url, data, ok, fail) {
    dd.httpRequest({
      url: url,
      method: 'POST',
      data: JSON.stringify(data),
      dataType: 'json',
      headers: {
        Authentication: app.globalData.token,
        'Content-Type': 'application/json;charset=UTF-8'
      },
      success: (res) => {
          if(res.data.status === 'failure') {
            fail(res.data.data)
          } else {
            ok(res.data.data)
          }
      },
      fail: (res) => {
          fail()
      }
    })
  },
  login(callback) {
    if (app.globalData.token) {
      callback()
      return
    }
    dd.getAuthCode({
        success:(res)=>{
            dd.httpRequest({
                url: urls.loginUrl(),
                method: 'POST',
                headers:{"Content-Type":"application/json"},
                data: JSON.stringify({authCode: res.authCode}),
                dataType: 'json',
                success: (res) => {
                  app.globalData.token = res.data.data
                  this.getMyInfo(callback)
                  // callback()
                },
                fail: (res) => {
                  // 非法authCode、用户名或密码错误、未绑定用户，都统一跳转到bind页面
                  dd.redirectTo({
                    url: '/pages/bind/bind'
                  })
                }
            });
        },
        fail: (err)=>{
          dd.alert({
              content: JSON.stringify(err)
          })
        }
    })
  },
  getMyInfo(callback) {
    this.get(urls.myInfo(), data => {
      app.globalData.userInfo = data
      this.getMyInstitutionInfo(callback)
    })
  },
  getMyInstitutionInfo(callback) {
    this.get(urls.myInstitutionInfo(), (data) => {
      app.globalData.instituteInfo = data
      callback()
    })
  },
};
